﻿using  ZenSystemService.Api.DTOs;
using ZenSystemService.Api.Common;
using System.Collections.Generic;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Common.Encryption;
using ZenSystemService.Api.Services;
namespace ZenSystemService.Api.Business
{
    public class ZenBrightBusiness
    {
        private readonly UserService _userService;

        public ZenBrightBusiness(UserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 获取测试列表
        /// </summary>
        /// <returns></returns>
        public ZenBrightResponse<List<ZenBrightTestItemDetail>> GetTestList()
        {
            string msg;
            bool isSuccess;
            // ZenBrightResponse<List<ZenBrightTestItemDetail>> _zenBrightResponse = ApiUtility.RequestPostApi<ZenBrightResponse<List<ZenBrightTestItemDetail>>>("api/ZenSystem/GetTestList", null, out isSuccess, out msg);
            ZenBrightResponse<List<ZenBrightTestItemDetail>> _zenBrightResponse = ApiUtility.RequestGetApi<ZenBrightResponse<List<ZenBrightTestItemDetail>>>("api/ZenSystem/GetTestList", out isSuccess, out msg);
            return _zenBrightResponse;
        }


        /// <summary>
        /// 发送默认测试订单
        /// </summary>
        /// <param name="zenBrightUserId"></param>
        public async Task<bool> SendDefaultTestOrder(int zenBrightUserId,int TraineeId,int currentUserId)
        {
            ZenBrightResponse<List<ZenBrightTestItemDetail>> _zenBrightResponse=this.GetTestList();
            if (_zenBrightResponse == null)
                return false;
            if (_zenBrightResponse.Status == false)
                return false;
            List<ZenBrightTestItemDetail> ZenBrightTestItemDetailList = _zenBrightResponse.Data;
            if (ZenBrightTestItemDetailList == null || ZenBrightTestItemDetailList.Count == 0)
                return false;

            string ids = "";
            for (int i = 0; i < ZenBrightTestItemDetailList.Count; i++)
            {
                if (ZenBrightTestItemDetailList[i].Name != "推理能力" && ZenBrightTestItemDetailList[i].Name != "语言理解能力测试")
                {
                    ids += ZenBrightTestItemDetailList[i].Id.ToString() + ",";
                }
            }
            ids= ids.Trim(',');

            SendTestOrderRequest _sendTestOrderRequest=new SendTestOrderRequest();
            _sendTestOrderRequest.ids = ids;
            _sendTestOrderRequest.uid = zenBrightUserId;
            string msg;
            bool isSuccess;
            ZenBrightResponse<string> zenBrightResponse = ApiUtility.RequestPostApi<ZenBrightResponse<string>>("api/ZenSystem/SendTestOrder", _sendTestOrderRequest, out isSuccess, out msg);
            if (isSuccess == false)
                return false;
            if (zenBrightResponse.Status == false)
                return false;

            string[] dataArr= zenBrightResponse.Data.Split("=");
            string code = dataArr[1];

            ZenBrightTestOrder _zenBrightTestOrder=new ZenBrightTestOrder();
            _zenBrightTestOrder.TraineeId = TraineeId;
            _zenBrightTestOrder.Code= code;
            _zenBrightTestOrder.CreatedUserID = currentUserId;
            _zenBrightTestOrder.CreatedDate = DateTime.Now;
            bool saveIsSuccess =await _userService.AddZenBrightTestOrder(_zenBrightTestOrder);
            return saveIsSuccess;
        }

    }
}
